package net.ojbk.jiaofu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.ojbk.jiaofu.po.SyTask;
import net.ojbk.jiaofu.service.PublishService;
import net.ojbk.util.FileTool;
import net.ojbk.util.StringTool;

/**
 * Servlet implementation class PublishZip
 */
@WebServlet("/PublishZip")
public class PublishZipServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PublishZipServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 控制 是否正确访问
				request.setCharacterEncoding("UTF-8");
				response.setContentType("text/html; charset=UTF-8");
				PrintWriter out = response.getWriter();
				HttpSession session=request.getSession();
				// 前奏 ↑ ↑ ↑ ↑ ↑
				// 判断是否登录 ↓
				if (!(session != null && session.getAttribute("login") != null && (boolean)session.getAttribute("login"))) { // 如果未登录
					String message = "<a href=\"./"
							+ FileTool.getFileName(request.getHeader("Referer"))
							+ "\">&nbsp;&nbsp;&nbsp;Return</a>";
					out.write("not login. <br/>" + message);
					System.out.println(FileTool.getFileName(request.getHeader("Referer")));
					return ;
				}
				// 获取 客户端 基本信息
				String userId = (String)session.getAttribute("myId");
				
				// 获取 post 参数 来构造 业务逻辑的 参数
				SyTask task = new SyTask();
				task.setUserId(userId);
				task.setTitle(request.getParameter("title"));
				task.setContent(request.getParameter("content"));
				
				// begin--获取结束时间
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				Date date = null;
				try {
					date = format.parse(request.getParameter("endTime").replaceAll("T", " "));
				} catch (ParseException e) {
					Calendar calendar = new GregorianCalendar();
					calendar.setTime(new Date());
					calendar.add(Calendar.DATE, 7); // 自动增加 7天
					date = calendar.getTime();
				}
			    task.setEndTime(date);
				// end	--获取结束时间
				
			    task.setTaskType(2); // 设置为 image
			    task.setFileFormat(request.getParameter("fileFormat"));
			    task.setBasicInfo(StringTool.toOneString(request.getParameterValues("basicInfo")));
			    
				// 执行业务逻辑
			    String statusCode = PublishService.publisTask(task, request.getParameter("grade"), request.getParameter("college"), request.getParameter("cls"), 2);
				
				// 重定向访问
					// 重定向到收集页面
				response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
				response.setHeader("Location", "./task.jsp?taskId=" + statusCode);
			}

}
